package com.bilibili.juc.completableFuture;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
 * @author zuife
 * @description
 * @since 2024-04-20 13:45:16
 */
public class CompletableFutureMallDemo {
    static List<NetMall> list = Arrays.asList(
            new NetMall("jd"),
            new NetMall("tb"),
            new NetMall("pdd"),
            new NetMall("gome"),
            new NetMall("dangdang"),
            new NetMall("jd"),
            new NetMall("tb"),
            new NetMall("pdd"),
            new NetMall("gome")
    );

    public static List<String> getPrice(List<NetMall> list, String productName) {
        /**
         * 《mysql》in taobao price is 90.43
         */
        return list
                .stream()
                .map(netMall -> String.format("%s in %s price is %.2f"
                        , productName
                        , netMall.getNetMallName()
                        , netMall.calcPrice(productName)
                ))
                .collect(Collectors.toList());
    }

    public static List<String> getPriceByCompletableFuture(List<NetMall> list, String productName) {
        return list
                .stream()
                .map(netMall -> CompletableFuture.supplyAsync(() -> String.format("%s in %s price is %.2f"
                                        , productName
                                        , netMall.getNetMallName()
                                        , netMall.calcPrice(productName)
                                )
                        )
                ).collect(Collectors.toList())
                .stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("模拟电商购物");

        System.out.println(ThreadLocalRandom.current().nextDouble() * 2 + "mysql".charAt(0));
        long startTime = System.currentTimeMillis();
        //List<String> list1 = getPrice(list, "mysql");
        List<String> list1 = getPriceByCompletableFuture(list, "mysql");
        for (String s : list1) {
            System.out.println(s);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("总耗时：" + (endTime - startTime));

    }
}

@Data
@NoArgsConstructor
@Accessors(chain = true)
class NetMall {

    @Getter
    private String netMallName;

    public NetMall(String netMallName) {
        this.netMallName = netMallName;
    }

    public double calcPrice(String productName) {
        /**
         * 暂停1秒钟
         */
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ThreadLocalRandom.current().nextDouble() * 2 + productName.charAt(0);
    }
}
